package com.excel.service;

import com.excel.entity.Excel;
import com.excel.mapper.ExcelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class ExcelService {
    private final ExcelMapper excelMapper;

    @Autowired
    public ExcelService(ExcelMapper excelMapper) {
        this.excelMapper = excelMapper;
    }

    public List<Excel> findAll() {
        return excelMapper.findAll();
    }

    @Transactional
    public Excel save(Excel excel) {
        if (excel.getId() == null || excel.getId() == 0) {
            excelMapper.insert(excel);
        } else {
            excelMapper.update(excel);
        }
        return excel;
    }

    public void deleteById(Integer id) {
        excelMapper.deleteById(id);
    }

    public Optional<Excel> findById(Integer id) {
        Excel excel = excelMapper.selectById(id);
        return Optional.ofNullable(excel);
    }

    public List<Excel> searchByProjectName(String keyword) {
        return excelMapper.findByProjectNameContaining(keyword);
    }


}
